package com.app.controller;

import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.model.CategoryModel;
import com.app.service.AdminUserService;
import com.app.service.CategoryService;
import com.app.utils.CommonHelp;
import com.app.utils.StringTool;
import com.app.utils.TokenUtils;
import com.app.vo.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 公用数据配置
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AdminUserService adminUserService;

    @GetMapping("/getPerTree")//公共数据管理树
    public Result<Object> getCategoryTree1(String keyword, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }
        QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(keyword)) {
            qw.like("name", keyword);
        }
        List<CategoryModel> catModelList = categoryService.list(qw);//全查或者根据name查
        if (catModelList == null) {
            return Result.error(200, "暂无数据");
        }
        List<CategoryModel> categoryTree = categoryService.getCategoryTree(catModelList);
        return Result.success(categoryTree);
    }

    @GetMapping("/getCategoryTree")//公共数据管理树
    public Result<Object> getCategoryTree(String keyword, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }
        QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(keyword)) {
            qw.like("name", keyword);
        }

        List<CategoryModel> catModelList = categoryService.list(qw);//全查或者根据name查
        if (catModelList == null) {
            return Result.error(200, "暂无数据");
        }

        QueryWrapper<CategoryModel> qwPid =new QueryWrapper<>();
        qwPid.eq("pid",0);
        List<CategoryModel> categoryTree = categoryService.list(qwPid);
        for (CategoryModel categoryModel : categoryTree) {
            QueryWrapper<CategoryModel> childQW=new QueryWrapper<>();
            childQW.eq("pid",categoryModel.getId());
            List<CategoryModel> childCategory = categoryService.list(childQW);
            categoryModel.setChild(childCategory);


        }

//        List<CategoryModel> categoryTree = categoryService.getCategoryTree(catModelList);
        return Result.success(categoryTree);
    }

    //数据管理分页查询
    @GetMapping("/getCategoryList")
    public Result<Object> getCategoryList(@RequestParam("page") int page,
                                          @RequestParam("limit") int limit,
                                          @RequestParam(required = false) String name, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }
        CategoryVo categoryVoList = categoryService.getCategoryList(page, limit, name);
        categoryService.getCategoryList(page, limit, name);
        if (categoryVoList == null) {
            return Result.error(200, "暂无数据");
        }
        return Result.success(categoryVoList);
    }

    //数据管理分页查询
    @GetMapping("/getCategoryById")
    public Result<Object> getCategoryById(Integer id, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }
        CategoryModel categoryModel = categoryService.getById(id);
        return Result.success(categoryModel);
    }

    @PostMapping("/addAndUpdateCategory")//    新增/修改数据管理
    public Result<Object> addAndUpdateCategory(@RequestBody CategoryModel categoryModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }
        CategoryModel category = categoryService.getById(categoryModel.getId());
        if (category != null) {
            categoryModel.setUpdateTime(CommonHelp.getNowTimestamp());
            categoryService.updateById(categoryModel);
            return Result.success("修改成功");
        } else {
            categoryModel.setCreateTime(CommonHelp.getNowTimestamp());
            if (categoryModel.getPid() == null) {
                categoryModel.setPid(0);
            }
            categoryService.save(categoryModel);
            return Result.success("添加成功");
        }
    }

    @GetMapping("/updateCategoryStatus")//修改状态
    public Result<Object> updateStatus(@RequestParam("id") Integer id,
                                       @RequestParam("status") Integer status, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }

        CategoryModel model = new CategoryModel();
        model.setId(id);
        model.setStatus(status);
        categoryService.updateById(model);
        return Result.success("修改成功");
    }

    @GetMapping("/deleteCategory")//删除数据
    public Result<Object> deleteCategory(@RequestParam("id") Integer id, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }

        CategoryModel model = categoryService.getById(id);
        if (model == null) {
            return Result.error(500, "该条数据不存在");
        } else {
            if (model.getPid() == 0) {
            categoryService.deleteAll(model.getId());
            }
            categoryService.removeById(id);
            return Result.success(null);
        }
    }

    @GetMapping("/selectCategoryByName")
    public Result<Object> selectCategoryByName(String name, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }

        QueryWrapper qw = new QueryWrapper();
        qw.eq("name", name);
        CategoryModel model = categoryService.getOne(qw);
        if (model == null) {
            return Result.error(500, "该条数据不存在");
        } else {
            CategoryVo vo = new CategoryVo();
            List<CategoryModel> categoryModelList = categoryService.getCategoryByPid(model.getId());
            vo.setList(categoryModelList);
            return Result.success(vo);
        }
    }

}
